<template>
  <div class="personalized-recommendations">
    <div class="header">
      <h4>个性化推荐</h4>
      <el-button text @click="refreshRecommendations" :icon="Refresh">刷新</el-button>
    </div>

    <div class="recommendations-list">
      <el-card 
        v-for="(item, index) in recommendations"
        :key="index"
        class="recommendation-card"
        shadow="hover"
      >
        <div class="card-header">
          <div class="card-title">
            <el-icon class="card-icon"><component :is="item.icon" /></el-icon>
            <span>{{ item.title }}</span>
          </div>
          <el-tag :type="item.type" size="small">{{ item.category }}</el-tag>
        </div>
        
        <div class="card-content">
          <p>{{ item.description }}</p>
          
          <div class="card-data" v-if="item.data">
            <div 
              v-for="(data, idx) in item.data"
              :key="idx"
              class="data-item"
            >
              <span class="data-label">{{ data.label }}：</span>
              <span class="data-value">{{ data.value }}</span>
            </div>
          </div>
          
          <div class="card-actions">
            <el-button type="primary" size="small" @click="handleAction(item)">
              查看详情
            </el-button>
            <el-button size="small" @click="dismissRecommendation(item.id)">
              不再显示
            </el-button>
          </div>
        </div>
      </el-card>
    </div>

    <el-empty v-if="recommendations.length === 0" description="暂无个性化推荐" />
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { Refresh, TrendCharts, ShoppingCart, Money, User, Setting } from '@element-plus/icons-vue'

const recommendations = ref([])

const loadRecommendations = () => {
  recommendations.value = [
    {
      id: 1,
      title: '您经常查看的商品',
      category: '商品',
      type: 'info',
      icon: ShoppingCart,
      description: '基于您的浏览历史，推荐以下您可能感兴趣的商品',
      data: [
        { label: '推荐商品数', value: '5种' },
        { label: '价格区间', value: '¥100-500' }
      ],
      action: { type: 'product', id: 'recommended' }
    },
    {
      id: 2,
      title: '销售数据分析建议',
      category: '分析',
      type: 'success',
      icon: TrendCharts,
      description: '建议您查看最近一周的销售数据分析报告',
      data: [
        { label: '数据更新时间', value: '1小时前' },
        { label: '数据量', value: '1000+条' }
      ],
      action: { type: 'analysis', id: 'sales' }
    },
    {
      id: 3,
      title: '财务提醒',
      category: '财务',
      type: 'warning',
      icon: Money,
      description: '您有3笔应收账款即将到期，请注意跟进',
      data: [
        { label: '到期金额', value: '¥50,000' },
        { label: '最早到期', value: '3天后' }
      ],
      action: { type: 'finance', id: 'receivable' }
    }
  ]
}

const refreshRecommendations = () => {
  loadRecommendations()
  ElMessage.success('推荐已刷新')
}

const handleAction = (item) => {
  ElMessage.info(`执行操作：${item.action.type} - ${item.action.id}`)
}

const dismissRecommendation = (id) => {
  recommendations.value = recommendations.value.filter(r => r.id !== id)
  ElMessage.success('已不再显示该推荐')
}

onMounted(() => {
  loadRecommendations()
})
</script>

<style scoped lang="scss">
.personalized-recommendations {
  padding: 20px;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;

  h4 {
    margin: 0;
    color: #303133;
  }
}

.recommendations-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
  gap: 20px;
}

.recommendation-card {
  transition: transform 0.2s;

  &:hover {
    transform: translateY(-4px);
  }

  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 12px;
  }

  .card-title {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 16px;
    font-weight: 500;
  }

  .card-icon {
    font-size: 18px;
    color: #409EFF;
  }

  .card-content {
    p {
      margin: 0 0 12px;
      color: #606266;
      line-height: 1.6;
    }

    .card-data {
      margin-bottom: 16px;
      padding: 12px;
      background: #f5f5f5;
      border-radius: 6px;
    }

    .data-item {
      margin: 4px 0;
      font-size: 13px;

      .data-label {
        color: #909399;
      }

      .data-value {
        color: #303133;
        font-weight: 500;
      }
    }

    .card-actions {
      display: flex;
      gap: 8px;
    }
  }
}
</style>
